package com.project.frba.bifront.client.view.properties;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;

public class AvailablePropertyPanel extends Composite {
	
	private Panel mainPanel = new Panel();
	private PropertySelector parent;
	private ListBox lbProperties = new ListBox();
	private Button toCol = new Button("Enviar a columnas");
	private Button toRow = new Button("Enviar a filas   ");
	
	public AvailablePropertyPanel() {
	}
	
	public AvailablePropertyPanel(String title, List<String> properties, PropertySelector parent) {
		//mainPanel.setTitle(title);
		mainPanel.setLayout(new HorizontalLayout(3));
		this.parent = parent;
		
		Panel buttonsPanel = new Panel();
		buttonsPanel.setLayout(new VerticalLayout(3));
		buttonsPanel.add(toCol);
		buttonsPanel.add(toRow);
		
		toCol.addListener(new ButtonListenerAdapter(){
			@Override
			public void onClick(Button button, EventObject e) {
				super.onClick(button, e);
				moveToColumnProperties();
			}
		});
		
		toRow.addListener(new ButtonListenerAdapter(){
			@Override
			public void onClick(Button button, EventObject e) {
				super.onClick(button, e);
				moveToRowProperties();
			}
		});
		
		
		
		lbProperties.setVisibleItemCount(5);
		lbProperties.setWidth("250");
		
		
		for (String propertyName : properties) {
			this.addProperty(propertyName);
		}
		lbProperties.addStyleName("font-size: 11px");
		
		FlexTable tableLayout = new FlexTable();
		tableLayout.setHTML(0, 0, "<b style=\"font-size: 11px\">" + title + "</b>");
		tableLayout.setWidget(1, 0, lbProperties);
		mainPanel.add(tableLayout);
		mainPanel.add(buttonsPanel);
		initWidget(mainPanel);
	}
	
	public void moveToColumnProperties(){
		String propertyName = lbProperties.getValue(lbProperties.getSelectedIndex());
		parent.moveToColumnProperties(propertyName);
		lbProperties.removeItem(lbProperties.getSelectedIndex());
	}
	
	public void moveToRowProperties(){
		String propertyName = lbProperties.getValue(lbProperties.getSelectedIndex());
		parent.moveToRowProperties(propertyName);
		lbProperties.removeItem(lbProperties.getSelectedIndex());
	}

	public void addProperty(String propertyName) {
		lbProperties.addItem(parent.getLabelForProperty(propertyName), propertyName);
	}

	public List<String> getProperties() {
		List<String> properties = new ArrayList<String>();
		for (int i = 0; i < lbProperties.getItemCount(); i++) {
			properties.add(lbProperties.getValue(i));
		}
		return properties;
	}
	
	
	
}
